package com.etc.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Users;

@Controller
@RequestMapping(value = "restapi")
public class UserControllerRest {
	@Resource(name = "users")
	private Users u;

	// restapi/users
	// @ResponseBody 响应结果为json格式的数据
	@RequestMapping(value = "users", method = RequestMethod.GET)
	@ResponseBody
	public Users getUser() {
		return u;
	}

	@RequestMapping(value = "users/{id}", method = RequestMethod.GET)
	@ResponseBody // 设置响应主体 响应结果为json格式的数据
	public Users getUserById(@PathVariable(value = "id") int id) {
		System.out.println("rest id:" + id);
		return u;
	}

	@RequestMapping(value = "users", method = RequestMethod.POST)
	@ResponseBody // 设置响应主体 响应结果为json格式的数据
	public void addUser(@RequestBody Users user) {// @RequestBody设置传递过来的参数为json格式
		System.out.println("rest user:" + user);
	}

	@RequestMapping(value = "users/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public void updateUser(@PathVariable(value = "id") int id, @RequestBody Users user) {
		System.out.println("rest update user:" + user + ",id:" + id);
	}

	@RequestMapping(value = "users/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUser(@PathVariable(value = "id") int id) {
		System.out.println("rest delete id:" + id);
	}

}
